import store from '@/store'
import {getToken, removeToken, setToken} from '@/libs/token'
import * as loginApi from '@/api/loginApi'
import * as userApi from '@/api/userApi'
import defaultAvatar from '_a/images/common/avatar.png'

export default {
    state: {
        isLogin: JSON.parse(localStorage.getItem('isLogin')),
        loginId: JSON.parse(localStorage.getItem('loginId')),
        token: getToken(),
        username: '',
        avatar: '',
        role: '',
        permissions: [],
        dictTypes: {}
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
            setToken(token)
        },
        SET_IS_LOGIN(state, data) {
            state.isLogin = data
            localStorage.setItem('isLogin', JSON.stringify(state.isLogin))
        },
        SET_LOGIN_ID: (state, loginId) => {
            state.loginId = loginId
            localStorage.setItem('loginId', JSON.stringify(state.loginId))
        },
        SET_USERNAME: (state, username) => {
            state.username = username
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLE: (state, role) => {
            state.role = role
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        }
    },
    actions: {
        /**
         * 获取用户信息
         * @param commit
         * @returns {Promise<unknown>}
         */
        getUserDetail({commit}) {
            return new Promise((resolve, reject) => {
                const loginId = store.state.user.loginId
                console.log('当前用户ID - loginId : ' + loginId)
                userApi
                    .getUserDetail(loginId)
                    .then((res) => {
                        console.log('=============获取用户信息=============')
                        console.log(res)
                        const avatar =
                            res.data.userDTO.avatar === '' || res.data.userDTO.avatar == null
                                ? defaultAvatar
                                : res.data.userDTO.avatar
                        if (res.data.roles) {
                            // 验证返回的roles是否是一个非空数组
                            commit('SET_ROLE', res.data.roles)
                            commit('SET_PERMISSIONS', res.data.permissions)
                        } else {
                            commit('SET_ROLE', 'ROLE_DEFAULT')
                        }
                        commit('SET_USERNAME', res.data.userDTO.username)
                        commit('SET_AVATAR', avatar)
                        resolve(res)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        /**
         * 退出系统
         * @param commit
         * @param state
         * @returns {Promise<unknown>}
         */
        logout({commit, state}) {
            return new Promise((resolve, reject) => {
                loginApi
                    .logout(state.token)
                    .then(() => {
                        commit('SET_TOKEN', '')
                        commit('SET_ROLE', null)
                        commit('SET_PERMISSIONS', [])

                        localStorage.removeItem('isLogin')
                        localStorage.removeItem('loginId')
                        removeToken()
                        resolve()
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        }
    }
}
